<?php
namespace app\common\repositories\apps;


use app\common\repositories\BaseRepository;
use app\common\dao\apps\AppsAppsDao;
use think\facade\Db;

class AppsAppsRepository extends BaseRepository
{
    protected $dao;
    public function __construct(){
        $this->dao = new AppsAppsDao();
    }

    /**
     * @desc: create one AppsAccess
     * @param varchar $appkey 
     * @param varchar $device 
     * @param varchar $custom 
     * @author: generate
     */
    public function createOneAppsApps(
        $appkey,
        $device = '',
        $custom = '',
        $fingerprint = '',
        $version = ''
    ) {
        
        // to do  some check
        $data = compact('appkey','device','fingerprint','version');

        $data['is_del'] = 0;
        $data['create_at'] =  date('Y-m-d H:i:s');

        $id = Db::transaction(function () use($data) {
            $id = $this->create($data);
            // todo other

            return $id;
        });

        return ['code' => 200, 'msg' => 'ok', 'data' => ['id' => $id]];
    }

        /**
     * @desc: statistics
     * @author: xiaohp
     * @param {*} $app_key
     */
    public function getAppStatistics($app_key){
        $where = ['appkey_key' => $app_key,'is_del' => 0,'status'=>0];
        $ios_count = $this->dao->search($where)->where('device->isIOS','true')->count();
        $android_count = $this->dao->search($where)->where('device->isAndroid','true')->count();
        $repet_count = $this->dao->search($where)->group('fingerprint')->count();
        $today_count = $this->dao->search($where)->where('created_at','>',date('Y-m-d') . ' 00:00:01')->count();
        
        $list = [];
        $list[] = [
            'title' => 'iOS',
            'value' => $ios_count
        ];
        $list[] = [
            'title' => 'Android',
            'value' => $android_count
        ];
        $list[] = [
            'title' => '去重',
            'value' => $repet_count
        ];
        $list[] = [
            'title' => '今日新增',
            'value' => $today_count
        ];
        return $list;
    }

}